package com.panda.pandawash.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.bean.RegisterBean;
import com.panda.pandawash.util.HttpUrlUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.panda.pandawash.R;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.UserBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.util.WsUtils;
import com.panda.pandawash.view.StatusBarCompat;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;


@ContentView(R.layout.activity_forget_pswd)
public class ForgetPswdActivity extends Activity implements View.OnClickListener {
    private static String TAG = "API_TAG";
    private RegisterBean mRegisterBean;

    @ViewInject(R.id.edit_pswd_phone_number)
    private EditText editPhoneNumber;

    @ViewInject(R.id.edit_pswd_sms_number)
    private EditText editSMSNumber;

    @ViewInject(R.id.edit_pswd_password)
    private EditText editPassWord;

    @ViewInject(R.id.edit_pswd_password_repeat)
    private EditText editPassWordRepeat;

    @ViewInject(R.id.btn_get_pswd_sms_number)
    private Button btnGetSMSNumber;

    @ViewInject(R.id.btn_pswd_register)
    private Button btnRegister;

    @ViewInject(R.id.btn_pswd_return)
    private ImageView mBack;

    private int time = 60;
    private String myphonenumber;
    private String mypassword;
    private String mypasswordrepeat;
    private String mycheckcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT>20) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }
        x.view().inject(this);

        btnGetSMSNumber.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
        mBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_get_pswd_sms_number:
                getIdCode();
                break;
            case R.id.btn_pswd_register:
                changePassword();
                break;
            case R.id.btn_pswd_return:
                excuteBack();
            default:
                break;
        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        excuteBack();

    }

    private void getIdCode() {
        String userAccount = editPhoneNumber.getText().toString().trim();
        if (!WsUtils.isPhoneNumber(userAccount)) {
            Toast.makeText(this, R.string.registe_incorrect_username, Toast.LENGTH_SHORT).show();
            return;
        }

        btnGetSMSNumber.setClickable(false);
        time = 60;
        runnable.run();

        HttpUtil.getIdCode(new SendIdCodeCallback(), userAccount, userAccount);
    }

    private void changePassword() {

        myphonenumber = editPhoneNumber.getText().toString().trim();
        mypassword = editPassWord.getText().toString().trim();
        mypasswordrepeat = editPassWordRepeat.getText().toString().trim();
        mycheckcode = editSMSNumber.getText().toString().trim();

        if (!WsUtils.isPhoneNumber(myphonenumber)) {
            Toast.makeText(this, R.string.registe_incorrect_username, Toast.LENGTH_SHORT).show();
            editPhoneNumber.requestFocus();
            return;
        }

        if(TextUtils.isEmpty(mycheckcode)){
            Toast.makeText(this, R.string.registe_idcode_empty, Toast.LENGTH_SHORT).show();
            editSMSNumber.requestFocus();
            return;
        }

        if (!WsUtils.isMatch(WsUtils.REGEX_PASSWORD,mypassword)) {
            Toast.makeText(this, R.string.registe_password_legal, Toast.LENGTH_SHORT).show();
            editPassWord.requestFocus();
            return;
        }

        if((!mypassword.equals(mypassword.trim()))||(!mypasswordrepeat.equals(mypasswordrepeat.trim()))){
            Toast.makeText(this, R.string.registe_password_have_space, Toast.LENGTH_SHORT).show();
            editPassWord.requestFocus();
            return;
        }
        if(!mypassword.trim().equals(mypasswordrepeat.trim())){
            Toast.makeText(this, R.string.registe_password_not_same, Toast.LENGTH_SHORT).show();
            editPassWord.requestFocus();
            return;
        }

        HttpUtil.forceChangePSWWithIdCode(new ForceChangePSWCallback(), myphonenumber, myphonenumber, mypassword,mycheckcode);
    }

    private void excuteBack(){
        Intent mIntent = new Intent(ForgetPswdActivity.this, LoginActivity.class);
        ForgetPswdActivity.this.startActivity(mIntent);
        ForgetPswdActivity.this.finish();
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            time--;
            btnGetSMSNumber.setText("" + time);
            handler.postDelayed(this, 1000);
            if (time <= 0) {
                handler.removeCallbacks(runnable);
                btnGetSMSNumber.setClickable(true);
                btnGetSMSNumber.setText("获取验证码");
            }
        }
    };

    private class ForceChangePSWCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            Log.d(TAG, "ForceChangePSWCallback statusCode = " + statusCode);
            Log.d(TAG, "ForceChangePSWCallback onSuccess = " + content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, "user");
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    Log.d(TAG, "code = " + code);
                    if(code.equals("0")){
                        showMessage(getString(R.string.change_psw_success));
                        finish();
                    }else if(code.equals("99")){
                        showMessage(message);
                    }
                }
            } else {
                showMessage(getString(R.string.change_psw_fail));
            }
        }
    }

    private void showMessage(String string) {
        Toast toast = Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    private class SendIdCodeCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            Log.d(TAG, "SendIdCodeCallback statusCode = " + statusCode);
            Log.d(TAG, "SendIdCodeCallback onSuccess = " + content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, "user");
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    Log.d(TAG, "code = " + code);
                    if(code.equals("0")){
                        showMessage(getString(R.string.registe_idcode_success));
                    }else if(code.equals("99")){
                        showMessage(message);
                    }
                }
            } else {
                showMessage("registe_idcode_fail");

            }
        }
    }

}
